package com.zwz.crawler.knowledge;


import com.zwz.constant.Constant;
import com.zwz.entity.Trademark;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.List;

/**
 * 获取企业的知识产权信息
 */
@Slf4j
public class TungKnowledge {

    public static List<Trademark> startTungKnowledge(String enterpriseId ) {
//        String url = "https://sales.tungee.com/api/enterprise/577de19eadf21dc4/intellectual-property/tm";
        String url = "https://sales.tungee.com/api/enterprise/"+enterpriseId+"/intellectual-property/tm";
//        String cookie = "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=cc4a07fc-7bb9-40a4-96bc-2cbac34b658f; __last_enter_version=sales; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b84fe276-099f-4932-92b3-d54b92ae384b; _tx_sid=MTc0NTMwMTE0MzYyMjo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301094,1745302709; HMACCOUNT=B4E402DF057C895E; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuSXhVPSI.GujnjA.YBo4Zvc8vs4jgEmtioacknOQ9pE; tg_referrer_source=https%3A%2F%2Fuser.tungee.com%2F; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745311256; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNzAxZGRkY2E5NDE1MjJiNzdlODY0In0.GunNBw.92TTC4HS0MO6IPnlFJN9ynqpZeE; acw_tc=1a0c399617453735916127668e0057882b30e541b9ee5313e8a70b3e720285; accountCenterSessionId=.eJwljrFOxDAQRP_F9RW73o3tuw7IiYZLmkNIaaK1vVaIoiAlOUAg_p0IyhnNe5pv079mczIkFEpAjKCUHeWCJThUzBo1Fo9eSUKyMQMLK3NKyXshAAuS4AgAvgTKIbAII_lIUqRKnm2qgCRmriBjdMABrGPMrBZsUdrN2ZOag-nLoutgTtty04OZVXO_yrv221ufozkVmda9v626_F92AXzlIDlNGo-OojKitXZXpUHmWad99KFxz-sf0NbD0Hwl7Ornra27oXsAaK_N-PRy_mweL1sz3lWXazc19Zm68X4wP78weVR_.GungCg.gwSmZLbwl6QXChkjnchF0igH3gM"; // 完整 Cookie
//  .header("cookie", Constant.cookies)

        try {
            // 发送请求并模拟 HTTP/2 伪头
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .ignoreContentType(true)  // 允许非 HTML 响应
                    .timeout(30_000)
                    // 伪头映射（Jsoup 不支持直接设置，通过自定义头模拟）
                    .header("authority", "sales.tungee.com")
                    .header("path", "/api/enterprise/577de19eadf21dc4/intellectual-property/tm?start=0&end=5")
                    // 标准请求头
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("cookie", Constant.cookies)
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-information/intellectual-property")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    // 查询参数（等同载荷）
                    .data("start", "0")
                    .data("end", "5")
                    .execute();

           log.info("状态码: " + response.statusCode());
           log.info("响应体: " + response.body());

            //解析
            List<Trademark> list = JsonParserUtil.parseKnown(response.body());

            return list;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
